﻿using UnityEngine;
using System.Collections;

public class InteractiveMenu : MonoBehaviour {
	
	public bool ShowingActions;

	IInputController inputController;
	Transform interactionTriggerObj;
	ArrayList actionOptions;


	InteractiveObject owner;

	int currentSelectedItem = 0;
	Vector2 menuPosition;

	public float menuOptionWidth = 120;
	public float menuOptionHeight = 20;

	// Use this for initialization
	void Start () {
		Debug.Log("SHOWING INTERACTIVE MENU");
		owner = (InteractiveObject)GetComponent(typeof(InteractiveObject));
		if (owner == null) {
			Debug.LogError("Interactive Menu must have an owner");
		}

		actionOptions = owner.GetActions();
		if (actionOptions == null) {
			Debug.LogError("Interactive Menu must have an list of options. Check if InteractiveObject retuns some actions");
		}

		InteractiveObject obj = (InteractiveObject)this.transform.gameObject.GetComponent<InteractiveObject>();
		interactionTriggerObj = obj.actionTrigger;
		inputController = (IInputController)interactionTriggerObj.GetComponent(typeof(IInputController));
	}
	
	// Update is called once per frame
	void Update () {
		if (ShowingActions) {
			if (inputController.NavigateMenuUp()) {
				currentSelectedItem = MenuSelection(actionOptions, currentSelectedItem, "down");
			}
			
			if (inputController.NavigateMenuDown()) {
				currentSelectedItem = MenuSelection(actionOptions, currentSelectedItem, "up");
			}
			
			if (inputController.SecondaryActionButtonDown()) {   
				HandleSelection();
			}
		}
	}

	public void SetPositionReference(Vector2 position) {
		menuPosition = position;
	}

	public float GetHeightWithOptions() {
		return actionOptions.Count*menuOptionHeight + menuOptionHeight/2;
	}

	public void StopAllActions() {
		foreach (IInteractiveAction action in actionOptions) {
			action.Stop();
		}
	}

	int MenuSelection (ArrayList menuItems, int selectedItem, string direction) {
		int returnItem = currentSelectedItem;
		if (direction == "down") {
			if (selectedItem == 0) {
				returnItem = menuItems.Count - 1;
			} else {
				returnItem -= 1;
			}
		}
		
		if (direction == "up") {
			if (selectedItem == menuItems.Count - 1) {
				returnItem = 0;
			} else {
				returnItem += 1;
			}
		}
		
		return returnItem;
	}

	void HandleSelection()
	{
		IInteractiveAction currentSelectedAction = (IInteractiveAction)actionOptions[currentSelectedItem];
		GUI.FocusControl(currentSelectedAction.GetActionName());

		ShowingActions = false;
		currentSelectedAction.Execute();
	}


	void OnGUI() {
		Vector3 pos = Camera.main.WorldToScreenPoint(menuPosition);
		pos.y = (Screen.height - pos.y) - menuOptionHeight/2;

		bool actionKeyDown = inputController.ActionButtonDown();
		bool cancelActionDown = inputController.CancelButtonDown();
		if (actionKeyDown && !ShowingActions) {
			ShowingActions = true;

		}
		else if (cancelActionDown && ShowingActions) {
			ShowingActions = false;
		}



		if (GUI.Button(new Rect(pos.x, pos.y, menuOptionWidth, menuOptionHeight), "Interact (Press A)")) {}

		if (ShowingActions) {
			for (int i = 0; i < actionOptions.Count; i++) {
				IInteractiveAction action = (IInteractiveAction)actionOptions[i];
				string actionName = action.GetActionName();
				float y = pos.y + (i+1) * 20;

				if (currentSelectedItem == i)
				{
					GUI.contentColor = Color.red;
				}
				else {
					GUI.contentColor = Color.white;
				}

				if (GUI.Button(new Rect(pos.x ,y, menuOptionWidth + 20, menuOptionHeight), actionName))
				{
					currentSelectedItem = i;
					HandleSelection();
				}

				IInteractiveAction currentSelectedAction = (IInteractiveAction)actionOptions[currentSelectedItem];
				GUI.FocusControl(currentSelectedAction.GetActionName());
			}
		}
	}


}
